// pages/patrol_record/patrol_record.js
var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_imgUrl = app.globalData.applet_park_api_url_imgUrl;
const {
  requestDataparams
} = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:applet_park_api_url_imgUrl,
    isLeader: false,
    restaurantId:'',
    currenttDate: '',
    lists: [],
    id: '',
    dishsid:'',
    deatail:{},
    parentid:'',
    foodid:'',
    scheduledTime:'',
    packingCharges:'',
    prePagesType:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    var that = this;
    var date = new Date()
    var year = date.getFullYear()
    var month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
    var day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()
    this.data.currenttDate = year + '-' + month + '-' + day;
    
    if (options.restaurantId) {
      this.data.restaurantId = options.restaurantId;
    }
    if (options.dishsid) {
      this.data.dishsid = options.dishsid;
    }
    if(options.parentid){
      this.data.parentid = options.parentid;
    }
    if(options.scheduledTime){
      this.data.scheduledTime = options.scheduledTime;
    }
    if(options.foodid){
      this.data.foodid = options.foodid;
    }
    if(options.packingCharges){
      this.data.packingCharges = options.packingCharges;
    }
    if(options.pages){
      this.data.prePagesType = options.pages;
    }
    this.setData(this.data)
    this.loadData();
  },
  addShoppingCar(){//详情加入购物车  购买
    var that = this;
    var id = app.globalData.id || wx.getStorageSync("id");
    requestDataparams('/api/shoppingCarts', {
      sysUserId: id,
      restaurantId: Number.parseInt(that.data.restaurantId),
      dishTypeId: that.data.parentid,
      dishId: that.data.foodid,
      number: 1,
      scheduleTime: that.data.scheduledTime,
    }, 'POST').then(res => {
       if(res.data.code == 0){
        var mark = 'a-' + that.data.foodid + 'b-' +that.data.parentid;
        var obj = {
          price: that.data.detail.price,
          num: 1,
          mark: mark,
          name: that.data.detail.name,
          foodsimg: that.data.detail.image,
          foodid: that.data.foodid,
          parentid: that.data.parentid,
          openingTime: that.data.openingTime,
          endTime: that.data.endTime,
          packingCharges: that.data.packingCharges
        };
        var array = [];
        var array =array.concat(obj);
        wx.setStorageSync('carArray', array);
        wx.navigateTo({
          url : '../dishesList/dishesList'
        })
       }else{
        wx.showToast({
          title: res.data.message,
          icon: 'none'
        });
        return false 
       }
    }).catch(err => {
      
    });

  },
  timePro() {
    this.data.currenttDate = this.getBefOrAft(0, this.data.currenttDate);
    this.setData(this.data)
  },
  timeNaxt() {
    this.data.currenttDate = this.getBefOrAft(1, this.data.currenttDate);
    this.setData(this.data)
  },
  // 获取前天或者后一天  type: 0: 前一天 1：后一天
  getBefOrAft(type, date) {
    let forMatDate = date.replace(/-/g, '/')  // 格式化日期 2019-02-23 => 2019/02/23
    let d = new Date(forMatDate)    // new Date() 不能识别 2019-01-02  需转化为 2019/01/02
    if (type === 0) {
      d = +d - 1000 * 60 * 60 * 24
    } else if (type === 1) {
      d = +d + 1000 * 60 * 60 * 24  // d前面需要加上 +
    }
    d = new Date(d)
    return this.dateFormat(d)  // 格式化 将日期格式转化为我们需要展示格式
  },

  // 格式化日期，传入日期对象   =>2019-01-11  
  dateFormat(date) {
    let y = date.getFullYear()
    let m = date.getMonth() + 1
    let d = date.getDate()
    if (m <= 9) {
      m = '0' + m
    }
    if (d <= 9) {
      d = '0' + d
    }
    return y + '-' + m + '-' + d
  },

  loadData() {
    var that = this;

    wx.request({
      url: applet_park_api_url + '/api/dishes',
      method: 'get',
      data: {
        "id": that.data.dishsid
      },
      success: function (res) {
        

        if (res.data.code == 0) {

          that.data.detail = res.data.data;
          that.data.detail.image = that.Handleimg(that.data.detail.image)
          that.data.detail= res.data.data;
          that.setData(that.data)

          
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
        }
      },
      fail: function (res) {
        wx.showToast({
          title: '网络异常，请稍后重试',
        })
      }

    })
  },
  Handleimg(e) {
    let imglist = e.split(",");
    let imglists = [];
    let url = applet_park_api_url_imgUrl;
    for (let item of imglist) {

      let list = url +'/'+item
      imglists.push(list);
    }

    return imglists
  },
  selecttype(e) {
    this.setData({
      currenttype: e.currentTarget.dataset.index
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.loadData();
    wx.showToast({
      title: '刷新成功',
      icon: 'success',
      duration: 2000
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 获取前天或者后一天  type: 0: 前一天 1：后一天
  getBefOrAft(type, date) {
    let forMatDate = date.replace(/-/g, '/')  // 格式化日期 2019-02-23 => 2019/02/23
    let d = new Date(forMatDate)    // new Date() 不能识别 2019-01-02  需转化为 2019/01/02
    if (type === 0) {
      d = +d - 1000 * 60 * 60 * 24
    } else if (type === 1) {
      d = +d + 1000 * 60 * 60 * 24  // d前面需要加上 +
    }
    d = new Date(d)
    return this.dateFormat(d)  // 格式化 将日期格式转化为我们需要展示格式
  },

  // 格式化日期，传入日期对象   =>2019-01-11  
  dateFormat(date) {
    let y = date.getFullYear()
    let m = date.getMonth() + 1
    let d = date.getDate()
    if (m <= 9) {
      m = '0' + m
    }
    if (d <= 9) {
      d = '0' + d
    }
    return y + '-' + m + '-' + d
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;


    this.setData(this.data);
  },



})